/**
 * @param {number[]} nums
 * @return {number[][]}
 */
var permute = function (nums) {
  let res = []
  let n = nums.length
  let used = []
  let path = []
  function help(depth,path,used){
    if(depth === n){
      res.push([...path])
      return 
    }
    for (let i = 0; i < n; i++) {
      if(!used[i]){
        path.push(nums[i])
        used[i] = true
        help(depth+1,path,used) // 结束后需要重置
        used[i] = false
        path.pop()
      }
      
    }
  }
  help(0,path,used)
  return res
};

var nums = [1, 2, 3]
console.log(permute(nums))
